package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.SearchQueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public void saveOrUpdate(Strategy strategy) {
        if(strategy.getId()!=null){
            strategyMapper.updateByPrimaryKey(strategy);
        }else{
            strategyMapper.insert(strategy);
        }
    }

    @Override
    public List<Strategy> selectAll(QueryObject qo) {
        return strategyMapper.selectAll(qo);
    }


    @Override
    public PageInfo<Strategy> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Strategy> list = strategyMapper.selectForList(qo);

        //遍历数组将查询到的收藏数设置到每个对象中
        for (Strategy strategy : list) {
            strategy.setCollectionNum(strategyMapper.CountForCollection(strategy.getId()));
        }
        PageInfo<Strategy> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Strategy> queryNoReasonable(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),true,false,null);
        List<Strategy> list = strategyMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Strategy get(Long id) {
        Strategy strategy = strategyMapper.selectByPrimaryKey(id);
        //查询点赞数量
        int likeNum = strategyMapper.queryLikeCountByStrategyId(id);
        //设置点赞数量
        strategy.setLikeNum(likeNum);

        //获取当前用户的id
        if(UserContext.getCurrentUser()!= null){
            Long uid = UserContext.getCurrentUser().getId();
            //查询当前用户是否已点赞
            int isLike = strategyMapper.queryIsLike(uid, id);
            //处理查询结果
            if (isLike==1){
                strategy.setLike(true);
            }else {
                strategy.setLike(false);
            }
        }
        return strategy;
    }

    @Override
    public int CountForCollection(Long strategyId) {
        return strategyMapper.CountForCollection(strategyId);
    }

    @Override
    public PageInfo<Object> queryForList(SearchQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Strategy> list = strategyMapper.queryForList(qo);
        return new PageInfo(list);

    }

    @Override
    public int queryForCount(SearchQueryObject qo) {
        return strategyMapper.queryForCount(qo);
    }

    @Override
    public void updateBrowseNumById(Long strategyId) {
        strategyMapper.updateBrowseNumById(strategyId);
    }

}
